#include "include.h"
#include "display_dialogbox.h"

AT(.text.lcd.table)
const u32 db_menu_title_tbl[2] = {
    BMP_MENU_DB_TITLE_EN,
    BMP_MENU_DB_TITLE_ZH,
};

AT(.text.lcd.table)
const u32 db_menu_ok_tbl[2] = {
    BMP_MENU_DB_OK_EN,
    BMP_MENU_DB_OK_ZH,
};

AT(.text.lcd.table)
const u32 db_menu_cancel_tbl[2] = {
    BMP_MENU_DB_CANCEL_EN,
    BMP_MENU_DB_CANCEL_ZH,
};

void dialogbox_message(u16 msg)
{
    dialogbox_cb_t *s = &dialogbox_cb;

    switch (msg) {
        case KU_PLAY:
        case KU_PLAY_POWER:
        case KU_PLAY_MODE:
            if (s->index) {
                s->sta = DB_STA_OK_KEY;
            } else {
                s->sta = DB_STA_CANCEL_KEY;
            }
            break;

        case KU_PREV_VOL_DOWN:
        case KL_VOL_DOWN_PREV:
        case KU_PREV:
        case KU_NEXT_VOL_UP:
        case KL_VOL_UP_NEXT:
        case KU_NEXT:
            if (s->yesno_en) {
                s->index = (s->index) ? 0 : 1;
            }
            break;

        case EVT_UDISK_REMOVE:
            s->evt_dev = DEV_UDISK;
            s->sta = DB_STA_DEV_OUT;
            break;

        case EVT_SD_REMOVE:
            s->evt_dev = DEV_SDCARD;
            s->sta = DB_STA_DEV_OUT;
            break;

        case EVT_SD1_REMOVE:
            s->evt_dev = DEV_SDCARD1;
            s->sta = DB_STA_DEV_OUT;
            break;

        case EVT_UDISK_INSERT:
            s->evt_dev = DEV_UDISK;
            s->sta = DB_STA_DEV_IN;
            break;

        case EVT_SD1_INSERT:
            s->evt_dev = DEV_UDISK;
            s->sta = DB_STA_DEV_IN;
            break;

        case EVT_SD_INSERT:
            s->evt_dev = DEV_SDCARD1;
            s->sta = DB_STA_DEV_IN;
            break;

        default:
            func_message(msg);
            break;
    }
}

void dialogbox_display(void)
{
    dialogbox_cb_t *s = &dialogbox_cb;

    if (box_cb.disp_sta != GUI_BOX_NULL) {
        return;
    }

    if (s->index == s->disp_index) {
        return;
    }
    s->disp_index = s->index;
    lcd_buf_clear();

    if (s->title_en) {
        lcd_draw_image(RES_BUF_BMP_MENU_DAT, db_menu_title_tbl[sys_cb.lang_id], LINE_CENTER, 0);
    }
    lcd_line_update(0, 4);
    lcd_disp_str((char *)s->text, LINE_CENTER, 4, 0);
    if (s->yesno_en) {
        lcd_draw_image(RES_BUF_BMP_MENU_DAT, db_menu_ok_tbl[sys_cb.lang_id], 18, 6);
        lcd_draw_image(RES_BUF_BMP_MENU_DAT, db_menu_cancel_tbl[sys_cb.lang_id], 62, 6);
        if (s->index) {
            lcd_disp_invert(16, 6, 36, 2);
        } else {
            lcd_disp_invert(60, 6, 52, 2);
        }
    } else {
        lcd_draw_image(RES_BUF_BMP_MENU_DAT, db_menu_ok_tbl[sys_cb.lang_id], LINE_CENTER, 6);
        lcd_disp_invert(45, 6, 38, 2);
    }
    lcd_line_update(6, 2);
}

